import os

from oplatform.constant import DELIVERY_SYSTEM, CODERATE, TUNER, MODULATION, PSI_TABLE_FILTER
from oplatform.models import ConfigurationChannel
from oplatform.serializers.configuration import ConfigurationChannelSerializer


def write_config_file(obj):
    if os.path.exists('/var/www/channels/channel' + str(obj.id) + '.conf'):
        os.remove('/var/www/channels/channel' + str(obj.id) + '.conf')
    f = open('/var/www/channels/channel' + str(obj.id) + '.conf', 'w')
    delivery_system = obj.delivery_system
    coderate = obj.coderate
    tuner = obj.tuner
    modulation = obj.modulation
    psi_tables_filtering = obj.psi_tables_filtering
    delivery_system_conf = ''
    coderate_conf = ''
    tuner_conf = ''
    modulation_conf = ''
    psi_tables_filtering_conf = ''
    for delivery_system_item in DELIVERY_SYSTEM:
        if delivery_system == delivery_system_item[0]:
            delivery_system_conf = delivery_system_item[1]
    for coderate_item in CODERATE:
        if coderate == coderate_item[0]:
            coderate_conf = coderate_item[1]
    for tuner_item in TUNER:
        if tuner == tuner_item[0]:
            tuner_conf = tuner_item[1]
    for modulation_item in MODULATION:
        if modulation == modulation_item[0]:
            modulation_conf = modulation_item[1]
    for psi_tables_filtering_item in PSI_TABLE_FILTER:
        if psi_tables_filtering == psi_tables_filtering_item[0]:
            psi_tables_filtering_conf = psi_tables_filtering_item[1]
    f.write('delivery_system=' + delivery_system_conf + '\n')
    f.write('freq=' + str(obj.freq) + '\n')
    f.write('srate=' + str(obj.srate) + '\n')
    f.write('coderate=' + coderate_conf + '\n')
    f.write('tuner=' + tuner_conf + '\n')
    f.write('modulation=' + modulation_conf + '\n')
    f.write('multicast_iface4=' + obj.multicast_iface4 + '\n')
    f.write('multicast_ipv4=' + str(obj.multicast_ipv4) + '\n')
    f.write('common_port=' + str(obj.common_port) + '\n')
    f.write('multicast_ttl=' + str(obj.multicast_ttl) + '\n')
    f.write('autoconfiguration=' + obj.autoconfiguration + '\n')
    f.write('multicast_auto_join=' + str(obj.multicast_auto_join) + '\n')
    f.write('unicast=' + str(obj.unicast) + '\n')
    f.write('ip_http=' + obj.ip_http + '\n')
    f.write('dvr_buffer_size=' + str(obj.dvr_buffer_size) + '\n')
    f.write('dvr_thread=' + str(obj.dvr_thread) + '\n')
    f.write('dvr_thread_buffer_size=' + str(obj.dvr_thread_buffer_size) + '\n')
    f.write('scam_support=' + str(obj.scam_support) + '\n')
    f.write('ring_buffer_default_size=' + str(obj.ring_buffer_default_size) + '\n')
    f.write('decsa_default_delay=' + str(obj.decsa_default_delay) + '\n')
    f.write('send_default_delay=' + str(obj.send_default_delay) + '\n')
    f.write('port_http=' + obj.port_http + '\n')
    f.write('psi_tables_filtering=' + psi_tables_filtering_conf + '\n')
    f.write('autoconf_sid_list=' + obj.autoconf_sid_list + '\n')
    channels = ConfigurationChannel.objects.filter(configurationdata__id=obj.id)
    result = []
    for channel in channels:
        result.append(ConfigurationChannelSerializer(instance=channel).data)
    for channel_item in result:
        f.write('\n')
        f.write('new_channel' + '\n')
        f.write('ip=' + channel_item['ip'] + '\n')
        f.write('port=' + str(channel_item['port']) + '\n')
        f.write('service_id=' + str(channel_item['service_id']) + '\n')
        f.write('oscam=' + str(channel_item['oscam']) + '\n')
        f.write('name=' + channel_item['name'] + '\n')
    f.close()

